import 'package:boss/textStyle/text_style.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

import 'image/base_image.dart';

class NumberEditer extends StatefulWidget {
  final int value;
  final int minValue;
  final int maxValue;
  final ValueChanged<int> onChanged;

  const NumberEditer({
    Key key,
    this.value = 0,
    this.minValue = 0,
    this.maxValue,
    this.onChanged,
  }) : super(key: key);

  @override
  _NumberEditerState createState() => _NumberEditerState();
}

class _NumberEditerState extends State<NumberEditer> {
  bool get _isShowSubtract {
    return widget.value > 0;
  }

  _subtract() {
    if (widget.onChanged != null) {
      final value = widget.value - 1;
      if (value >= widget.minValue) {
        widget.onChanged(value);
      }
    }
  }

  _plus() {
    if (widget.onChanged != null) {
      final value = widget.value + 1;
      if (widget.maxValue == null || value <= widget.maxValue) {
        widget.onChanged(value);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        if (_isShowSubtract)
          GestureDetector(
            behavior: HitTestBehavior.translucent,
            child: Padding(
              padding: const EdgeInsets.all(4),
              child: BaseImage(
                'assets/image/cut_icon.png',
                width: 34.w,
                height: 34.w,
              ),
            ),
            onTap: _subtract,
          ),
        if (_isShowSubtract)
          Container(
            width: 70.w - 8,
            alignment: Alignment.center,
            child: Text(
              '${widget.value}',
              style: TextStyleConstant().normal_1_28,
            ),
          ),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          child: Padding(
            padding: const EdgeInsets.all(4),
            child: BaseImage(
              'assets/image/plus_icon.png',
              width: 34.w,
              height: 34.w,
            ),
          ),
          onTap: _plus,
        ),
      ],
    );
  }
}
